package com.hatricks.is.capturecode;

import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @packageName: com.hatricks.is.capturecode
 * @className: LocalCaptureMessage
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2024-12-03 15:36
 * @description: 本地消息存储
 */
public class LocalCaptureMessage extends CaptureMessage {

    // 5分钟缓存
    //private static final TimedCache<String, String> FIVE_TIME_CACHE = new TimedCache<>(5 * 1000L);
    private static final TimedCache<String, String> FIVE_TIME_CACHE = new TimedCache<>(5 * 60 * 1000L);

    private static final TimedCache<String, Integer> ONE_TIME_CACHE = new TimedCache<>(60 * 1000L);

    static {
        FIVE_TIME_CACHE.schedulePrune(1L);
        ONE_TIME_CACHE.schedulePrune(1L);
    }

    @Override
    protected Integer getCheckLimits(String key) {
        return ONE_TIME_CACHE.get(key, Boolean.FALSE);
    }

    @Override
    protected void saveCheckLimits(String key) {
        ONE_TIME_CACHE.put(key, 1);
    }

    @Override
    protected void saveCaptureCode(String captureCode, String phoneNumber, TemplateEnums templateEnums) {
        FIVE_TIME_CACHE.put(templateEnums.getName() + StrUtil.COLON + phoneNumber, captureCode);
    }

    @Override
    protected String getCaptureCode(String phoneNumber, TemplateEnums templateEnums) {
        return FIVE_TIME_CACHE.get(templateEnums.getName() + StrUtil.COLON + phoneNumber, Boolean.FALSE);
    }


    public static void main(String[] args) {
        LocalCaptureMessage localCaptureMessage = new LocalCaptureMessage();
        String captureCode = localCaptureMessage.sendMessage("15710481857", TemplateEnums.LOGIN_TEMPLATE);
        ThreadUtil.safeSleep(4000);
        localCaptureMessage.checkCaptureCode(captureCode, "15710481857", TemplateEnums.LOGIN_TEMPLATE);
        System.out.println("main线程执行完毕");
    }
}
